package com.yanqu.road.dao.impl.activity.mammonhall;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.mammonhall.MammonHallUserData;
import com.yanqu.road.entity.activity.mammonhall.MammonHallWorship;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class MammonHallUserDataDaoImpl extends TempDataObjectDao<MammonHallUserData> {

    public static String getMammonHallUserDataSql = "select * from `t_u_activity_mammon_hall_user` where `userId`=? and `activityId`=?";
    public MammonHallUserData getMammonHallUserData(long userId,int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMammonHallUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertMammonHallUserDataSql = "insert into `t_u_activity_mammon_hall_user`(userId,activityId,worshipMap,resetTime) values (?,?,?,?)";

    @Override
    protected boolean addData(MammonHallUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWorshipMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        if (getDb().executeNoneQuery(insertMammonHallUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateMammonHallUserDataSql = "update `t_u_activity_mammon_hall_user` set worshipMap=?,resetTime=? where userId=? and activityId=?";

    @Override
    protected boolean updateData(MammonHallUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWorshipMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateMammonHallUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected MammonHallUserData parseData(ResultSet rs) throws SQLException {
        MammonHallUserData data = new MammonHallUserData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setWorshipMap(JSON.parseObject(rs.getString("worshipMap"), new TypeReference<Map<Integer, MammonHallWorship>>() {}));
        data.setResetTime(rs.getLong("resetTime"));
        return data;
    }
}
